# Create your tests here.
from demo.demo_access_mgmt.models import Role
from demo_common_module.base_test_case import BaseApiTestCase


class AccessApiTestCase(BaseApiTestCase):
    model_name = '系统权限管理'
    stage = 'xxxxxx'

    def test_role_user(self):
        path = "/api/access/role/user/"
        function_name = 'test_role_user'

        # post
        input_data = {
            "role_id": 1,
            "user_ids": [1, 2, 3]
        }

        desc = '添加用户角色'
        self.post(path, self.model_name, function_name, desc, input_data, self.stage)

        # delete
        desc = '删除用户角色'
        self.delete(path, self.model_name, function_name, desc, input_data, self.stage)

    def test_role_permission(self):
        path = "/api/access/role/permission/"
        function_name = 'test_role_permission'

        # 插入一条角色信息
        role = Role()
        role.id = 2
        role.name = 'test'
        role.create_user = 'test'
        role.save()

        # post
        input_data = {
            "role_id": 2,
            "permission_ids": [1, 2, 3]
        }
        desc = '增加用户权限'
        self.post(path, self.model_name, function_name, desc, input_data, self.stage)

        # get
        input_data = {
            "role_id": 2
        }
        desc = '查询用户权限'
        self.get(path, self.model_name, function_name, desc, input_data, self.stage)

    def test_user_list(self):
        path = "/api/access/user/list/"
        function_name = 'test_user_list'
        input_data = {
            "user_id": "",
            "role_name": "",
            "location": "",
            "department": ""
        }

        desc = '查询用户列表'
        self.get(path, self.model_name, function_name, desc, input_data, self.stage)

    def test_permission_management(self):
        path = "/api/access/permission/management/"
        function_name = 'test_permission_management'
        # post
        input_data = {
            "parent_id": 0,
            "title": "test",
            "url": "/test",
            "name": "test_name",
            "icon": "test"
        }

        desc = '增加权限'
        result = self.post(path, self.model_name, function_name, desc, input_data, self.stage)
        input_data = result["data"]
        input_data["permission_id"] = input_data["id"]
        input_data["title"] = input_data["title"] + "-put"

        # get
        desc = '查询权限'
        self.get(path, self.model_name, function_name, desc, input_data, self.stage)

        # put
        desc = '修改权限'
        self.put(path, self.model_name, function_name, desc, input_data, self.stage)

        # delete
        desc = '删除权限'
        self.delete(path, self.model_name, function_name, desc, input_data, self.stage)

    def test_role_management(self):
        path = "/api/access/role/management/"
        function_name = 'test_role_management'
        # post
        input_data = {
            "role_name": "test",
            "create_user": "test"
        }

        desc = '增加角色'
        result = self.post(path, self.model_name, function_name, desc, input_data, self.stage)
        input_data = result["data"]
        input_data["role_id"] = input_data["id"]
        input_data["role_name"] = input_data["name"] + "-put"
        input_data["create_user"] = input_data["name"] + "-put"
        # get
        desc = '查询角色'
        self.get(path, self.model_name, function_name, desc, input_data, self.stage)

        # put
        desc = '修改角色'
        self.put(path, self.model_name, function_name, desc, input_data, self.stage)

        # delete
        desc = '删除角色'
        self.delete(path, self.model_name, function_name, desc, input_data, self.stage)
